public class LoanCalculator {
    private double principal; // 贷款总额（万元）
    private int months;       // 贷款时间（个月）
    private double annualRate; // 年利率

    // 构造函数
    public LoanCalculator(double principal, int months, double annualRate) {
        this.principal = principal;
        this.months = months;
        this.annualRate = annualRate;
    }

    // 计算等额本金还款法的总利息
    public double calculateEqualPrincipalInterest() {
        double monthlyRate = annualRate / 12 / 100; // 转换为月利率
        double totalInterest = (months * (months + 1) / 2) * principal * monthlyRate;
        return totalInterest;
    }

    // 计算等额本息还款法的总利息
    public double calculateEqualPrincipalAndInterest() {
        double monthlyRate = annualRate / 12 / 100; // 转换为月利率
        double totalInterest = (months * principal * monthlyRate * (1 + monthlyRate) /
                ((Math.pow(1 + monthlyRate, months) - 1))) - principal;
        return totalInterest;
    }

    // 主方法用于测试
    public static void main(String[] args) {
        double principal = 100; // 贷款总额（万元）
        int months = 12;        // 贷款时间（12个月）
        double annualRate = 5;  // 年利率（5%）

        LoanCalculator calculator = new LoanCalculator(principal, months, annualRate);

        double totalInterestEqualPrincipal = calculator.calculateEqualPrincipalInterest();
        System.out.printf("等额本金还款法的总利息: %.2f万元\n", totalInterestEqualPrincipal);

        double totalInterestEqualPrincipalAndInterest = calculator.calculateEqualPrincipalAndInterest();
        System.out.printf("等额本息还款法的总利息: %.2f万元\n", totalInterestEqualPrincipalAndInterest);
    }
}
